#include "server.hpp"

#define HOST "127.0.0.1"
#define USER "root"
#define PASSWD "123456"
#define DBNAME "online_gobang"
#define PORT 3306

void mysql_test()
{
    MYSQL *mysql = mysql_util::mysql_create(HOST, USER, PASSWD, DBNAME);
    const char *sql = "insert into student values (1,20,'唐晨')";
    mysql_util::mysql_exec(mysql, sql);
    mysql_util::mysql_destroy(mysql);
}

void json_test()
{
    Json::Value root;
    root["姓名"] = "唐晨";
    root["年龄"] = 21;
    root["成绩"].append(34.1);
    root["成绩"].append(46.1);
    root["成绩"].append(83.1);

    std::string str;
    json_util::serialize(root, str);
    // std::cout << str << std::endl;
    DLOG("%s", str.c_str());

    Json::Value values;
    json_util::deserialize(str, values);
    std::cout << values["姓名"].asString() << std::endl;
    std::cout << values["年龄"].asInt() << std::endl;
    int sz = values["成绩"].size();
    for (int i = 0; i < sz; i++)
    {
        std::cout << values["成绩"][i].asFloat() << std::endl;
    }
}

void string_test()
{
    std::string str = "hello,nihao,,,world,,";
    std::vector<std::string> vs;
    int n = string_util::splict(str, ",", vs);
    for (auto e : vs)
    {
        std::cout << e << std::endl;
    }
}

void file_test()
{
    const char *filename = "./makefile";
    std::string str;
    file_util::read(filename, str);
    DLOG("%s", str.c_str());
}

void db_test()
{
    user_exec ue(HOST, USER, PASSWD, DBNAME);
    Json::Value user;
    bool ret = ue.select_by_id(1, user);
    if (ret == false)
    {
        ELOG("select by name fail!");
    }

    std::string body;
    json_util::serialize(user, body);
    std::cout << body << std::endl;

    // user["username"] = "唐晨";
    // user["password"] = "123456";
    // ue.insert(user);
    // ue.lose(1);
    // if(ret == false)
    // {
    //     ELOG("login fail!");
    // }

    // std::string body;
    // json_util::serialize(user, body);
    // std::cout << body << std::endl;
}

void online_test()
{
    OlineUser_Manager om;
    uint64_t id = 1;
    wwserver_t::connection_ptr ws;
    om.enter_game_hall(id, ws);
    if (om.is_in_hall_user(id))
    {
        DLOG("IN HALL USER");
    }
    else
    {
        DLOG("NOT IN HALL USER");
    }

    om.exit_game_hall(id);
    if (om.is_in_hall_user(id))
    {
        DLOG("IN HALL USER");
    }
    else
    {
        DLOG("NOT IN HALL USER");
    }
}

int main()
{
    gobang_server gs(HOST, USER, PASSWD, DBNAME, PORT);
    gs.start(8084);
    return 0;
}